 //
//  StreetGuidDetailController.m
//  homeland
//
//  Created by Alawn on 13-10-21.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "StreetGuidDetailController.h"
#import "StreetGuidDetailInfoCell.h"
#import "MYGuideCommonDetailService.h"
#import "CommonUtil.h"

#define DETAIL_TEXTVIEW_WIDTH 210.0
@interface StreetGuidDetailController ()
@property (nonatomic, strong) MYGuideCommonDetailService *detailServer;
@end

@implementation StreetGuidDetailController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"指南详情";
    if (!self.item) {
        self.item = [_config objectForKey:@"dataItem"];
        if (self.item != nil) {
            self.detailServer = [[MYGuideCommonDetailService alloc] init];
            self.detailServer.delegate = self;
            self.detailServer.dataType = self.item.dataType;
            [self.detailServer loadDataWithKey:self.item.Id];
        }
    }
    NSLog(@"_config = %@",[_config objectForKey:@"dataItem"]);
    NSLog(@"self.item - %@",self.item.Id);
    NSLog(@"self.item - %@",self.item.dataType);

}

#pragma mark - Data Service
-(void)loadDataFinished
{
    if (self.detailServer.dataList.count > 0) {
        if ([self.item.dataType isEqualToString:@"SERVICE_ITEM"] && self.item.adminOrg == nil &&  self.item.serviceAgent == nil) {
            self.item = self.detailServer.dataList[0];
        }
 
    }
    [self stopLoadingView];
    [self.tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 12;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StreetGuidDetailInfoCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"StreetGuidDetailInfoCell" owner:self options:nil] objectAtIndex:0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.detailTextView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.titleLabel.scrollEnabled = NO;
    cell.detailTextView.scrollEnabled = NO;
    if (self.item) {
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"事项名称";
        cell.detailTextView.text = self.item.name;
    } else if (indexPath.row == 1){
        cell.titleLabel.text = @"主管部门";
        cell.detailTextView.text = self.item.adminOrg;
    } else if (indexPath.row == 2){
        cell.titleLabel.text = @"受理机构";
        cell.detailTextView.text = self.item.serviceAgent;
    } else if (indexPath.row == 3){
        cell.titleLabel.text = @"办理对象";
        cell.detailTextView.text = self.item.serviceObject;
    } else if (indexPath.row == 4){
        cell.titleLabel.text = @"办理依据";
        if (![_item.legalBasis isKindOfClass:[NSNull class]]) {
            cell.detailTextView.text = self.item.legalBasis;
        }
    } else if (indexPath.row == 5){
//         cell.titleLabel.frame = CGRectMake(10, 13, 100, 36);
        cell.titleLabel.text = @"办理条件";
        cell.detailTextView.text = self.item.conditions;
    } else if (indexPath.row == 6){
        cell.titleLabel.text = @"提交材料";
        cell.detailTextView.text = self.item.submitDocuments;
    } else if (indexPath.row == 7){
        cell.titleLabel.frame = CGRectMake(10, 8, 100, 36);
        cell.titleLabel.text = @"收费依据标准";

        if (![_item.charge isKindOfClass:[NSNull class]]) {
            cell.detailTextView.text = self.item.charge;
        } else {
            cell.detailTextLabel.text = @"";
        }
    } else if (indexPath.row == 8){
         cell.titleLabel.frame = CGRectMake(10, 8, 100, 36);
        cell.titleLabel.text = @"窗口办理流程";
        cell.detailTextView.text = self.item.windowProcess;
    } else if (indexPath.row == 9){
        cell.titleLabel.text = @"法定年限";
        cell.detailTextView.text = self.item.legalPeriod;
    } else if (indexPath.row == 10){
        cell.titleLabel.text = @"承诺期限";
        if (![_item.promisedPeriod isKindOfClass:[NSNull class]]) {
            cell.detailTextView.text = self.item.promisedPeriod;
        } else{
            cell.detailTextView.text = @"";
        }
    } else if (indexPath.row == 11){
        cell.titleLabel.text = @"办理时间";
        cell.detailTextView.text = self.item.timeCost;
    }
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.item) {
        
    
    if (indexPath.row == 0) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:[CommonUtil dealNullObject: self.item.name]  AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出事项名称的高度:%f -- %@",height,[CommonUtil dealNullObject: self.item.name]);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 1){
        CGFloat height = [self caculateHeightWithoutPaddingForString:[CommonUtil dealNullObject: self.item.adminOrg]  AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出事项名称的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 2) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.serviceAgent AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出受理机构的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 3) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.serviceObject AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理对象的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 4) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.charge AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理依据的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 5) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.conditions AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理条件的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 6) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.submitDocuments AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出提交材料的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 7) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.charge AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出提交材料的高度:%f",height);
        return MAX(46.0+26.0, height+26.0);
    }
    if (indexPath.row == 8) {
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.windowProcess AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出窗口办理流程的高度:%f",height);
        return MAX(46.0+26.0, height+26.0);
    }
    if (indexPath.row == 9){
        CGFloat height = [self caculateHeightWithoutPaddingForString:self.item.legalPeriod AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理依据的高度:%f",height);
        return MAX(44.0, height+26.0);
    }
    if (indexPath.row == 10){
        
        CGFloat height = [self caculateHeightWithoutPaddingForString:[CommonUtil dealNullObject: self.item.promisedPeriod] AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理依据的高度:%f",height);
    }
    if (indexPath.row == 11){

        CGFloat height = [self caculateHeightWithoutPaddingForString:[CommonUtil dealNullObject: self.item.timeCost] AndFontSize:17.0 AndWitdh:DETAIL_TEXTVIEW_WIDTH];
        NSLog(@"计算出办理依据的高度:%f",height);
    }
    }
    return 46.0;
}
@end
